/* main.cxx */

#include "main.hxx"

SDL_Surface *screen;
TTF_Font *font;

SDL_Event event;

yakwailog logger;

Uint32 TimeLeft(void)
{
    static Uint32 next_time = 0;
    Uint32 now, ret = 0;
    now = SDL_GetTicks();
    if (next_time <= now)
	{
        next_time = now + TICK_INTERVAL;
        return(ret);
    }
    ret = next_time - now;
    return(ret);
}


void game(void)
{
	unsigned int quit = 0;
	unsigned int moving_right = 0;
	unsigned int moving_left = 0;
	unsigned int moving_up = 0;
	unsigned int moving_down = 0;
	unsigned int redraw_screen = 1;
	unsigned int timer = SPRITE_TIMER_MAX;
	//SDL_Event event;
	SDL_Surface *player;
	SDL_Surface *temp;
	SDL_Rect player_pos;
	SDL_Rect selected_sprite;
	player_pos.x = screen->w / 2 - 16;
	player_pos.y = screen->h / 2 - 32;
	player_pos.w = 32;
	player_pos.h = 64;
	selected_sprite.x = 0;
	selected_sprite.y = 0;
	selected_sprite.w = 32;
	selected_sprite.h = 64;
	logger.init((char*)"aqfg", "aqfg.log", DEFAULT_LOGLEVEL);
	logger.log(0, "starting up %s.", AQFG_VERSION);
	if ((temp = IMG_Load("data/graphics/player.png")) == NULL) printf("player pic error!\n");
	player = SDL_DisplayFormatAlpha(temp);
	SDL_FreeSurface(temp);
	Scroll_LevelType * rpg_level = sli_return_level((char*)"level1.sli");
	Scroll * scroll_engine = new Scroll(25, 19, rpg_level);
	scroll_engine->Blit(screen);
	SDL_BlitSurface(player, &selected_sprite, screen, &player_pos);
//	SDL_Flip(screen);
	while(!quit)
	{
		if (SDL_PollEvent(&event))
		{
			if (event.type == SDL_KEYDOWN)
			{
				switch(event.key.keysym.sym)
				{
					case SDLK_ESCAPE:
						quit = 1;
					break;
					
					case SDLK_DOWN:
						moving_down = 1;
						moving_up = 0;
					break;
					
					case SDLK_UP:
						moving_up = 1;
						moving_down = 0;
					break;
					
					case SDLK_RIGHT:
						moving_right = 1;
						moving_left = 0;
					break;
					
					case SDLK_LEFT:
						moving_left = 1;
						moving_right = 0;
					break;
				}
			}
			if (event.type == SDL_KEYUP)
			{
				switch(event.key.keysym.sym)
				{
					case SDLK_DOWN:
						moving_down = 0;
						selected_sprite.x = 32;
						redraw_screen = 1;
					break;
					
					case SDLK_UP:
						moving_up = 0;
						selected_sprite.x = 96;
						redraw_screen = 1;
					break;
					
					case SDLK_RIGHT:
						moving_right = 0;
						selected_sprite.x = 0;
						redraw_screen = 1;
					break;
					
					case SDLK_LEFT:
						moving_left = 0;
						selected_sprite.x = 64;
						redraw_screen = 1;
					break;
				}
			}
			
		}
		if (moving_left || moving_right || moving_up || moving_down)
		{
			redraw_screen = 1;
			timer--;
			if (timer == 0) timer = SPRITE_TIMER_MAX;
		} else
		{
			timer = SPRITE_TIMER_MAX;
		}
		if (moving_left == 1)
		{
			if ((scroll_engine->left_edge_reached) || ((scroll_engine->right_edge_reached) && (player_pos.x > (screen->w / 2 - player_pos.w / 2))))
			{
				player_pos.x -= PLAYER_SPEED;
			} else
			{
				scroll_engine->Move(PLAYER_SPEED*(-1),0);
			}
			if (player_pos.x < 0) player_pos.x = 0;
//			selected_sprite.x = 64;
			if (timer > (SPRITE_TIMER_MAX/2))
			{
				selected_sprite.x = 256;
			} else
			{
				selected_sprite.x = 288;
			}
		}
		if (moving_right == 1)
		{
			if ((scroll_engine->right_edge_reached) || ((scroll_engine->left_edge_reached) && (player_pos.x < (screen->w / 2 - player_pos.w / 2))))
			{
				player_pos.x += PLAYER_SPEED;
			} else
			{
				scroll_engine->Move(PLAYER_SPEED,0);
			}
			if ((player_pos.x + player_pos.w) > screen->w) player_pos.x = screen->w - player_pos.w;
//			selected_sprite.x = 0;
			if (timer > (SPRITE_TIMER_MAX/2))
			{
				selected_sprite.x = 320;
			} else
			{
				selected_sprite.x = 352;
			}
		}
		if (moving_up == 1)
		{
			if ((scroll_engine->upper_edge_reached) || ((scroll_engine->lower_edge_reached) && (player_pos.y > (screen->h / 2 - player_pos.h / 2))))
			{
				player_pos.y -= PLAYER_SPEED;
			} else
			{
				scroll_engine->Move(0,PLAYER_SPEED*(-1));
			}
			if (player_pos.y < 0) player_pos.y;
			if (timer > (SPRITE_TIMER_MAX/2))
			{
				selected_sprite.x = 128;
			} else
			{
				selected_sprite.x = 160;
			}
		}
		if (moving_down == 1)
		{
			if ((scroll_engine->lower_edge_reached) || ((scroll_engine->upper_edge_reached) && (player_pos.y < (screen->h / 2 - player_pos.h / 2))))
			{
				player_pos.y += PLAYER_SPEED;
			} else
			{
				scroll_engine->Move(0,PLAYER_SPEED);
			}
			if ((player_pos.y + player_pos.h) > screen->h) player_pos.y = screen->h - player_pos.h;
			if (timer > (SPRITE_TIMER_MAX/2))
			{
				selected_sprite.x = 224;
			} else
			{
				selected_sprite.x = 192;
			}
		}
		if (redraw_screen)
		{
			scroll_engine->Blit(screen);
			SDL_BlitSurface(player, &selected_sprite, screen, &player_pos);
			SDL_Flip(screen);
			redraw_screen = 0;
		}
		SDL_Delay(TimeLeft());
//		printf("left %d right %d upper %d lower %d\n", scroll_engine->left_edge_reached, scroll_engine->right_edge_reached, scroll_engine->upper_edge_reached, scroll_engine->lower_edge_reached);
	}

	delete scroll_engine;
	sli_free_level(rpg_level);
	SDL_FreeSurface(player);
}

int main(void)
{
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
	{
		printf("there was a problem initialising sdl.\n");
		exit(-1);
	}
	atexit(SDL_Quit);
	screen = SDL_SetVideoMode(800, 600, VIDEO_BITS, VIDEO_MODE);
	if (screen == NULL) 
	{
		printf("there was a problem creating the sdl surface.\n");
    	exit(-1);
	}
	if (TTF_Init() == -1)
	{
		printf("there was a problem initialising sdl_ttf.\n");
		exit(-2);
	}
	font = TTF_OpenFont("data/fonts/VeraMono.ttf", 24);
	if (!font)
	{
		printf("there was a problem loading the font.\n");
		exit(-2);
	}
	SDL_WM_SetCaption("aqfg", "aqfg");
	game();
	SDL_FreeSurface(screen);
	return 0;
}
